//
//  AliMNSHttpBodyDecoder.m
//  AlibabaCloudMNSiOS
//
//  Created by David Chen on 28/03/2017.
//  Copyright © 2017 AliCloud. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AliMNSHttpBodyDecoder.h"
#import "XMLDictionary.h"
#import "AliMNSQueueMeta.h"
#import "AliMNSTopicMeta.h"
#import "AliMNSSubscription.h"

@implementation  AliMNSHttpBodyDecoder

+ (void) parseQueueMetaFromDict:(NSDictionary*) tmpDict
            withOutputQueueMeta:(AliMNSQueueMeta*) qMeta
{
    if([tmpDict objectForKey:@"QueueName"])
    {
        qMeta.queueName = [tmpDict objectForKey:@"QueueName"];
    }

    if([tmpDict objectForKey:@"ActiveMessages"])
    {
        qMeta.activeMessages = [[tmpDict objectForKey:@"ActiveMessages"] integerValue];
    }

    if([tmpDict objectForKey:@"CreateTime"])
    {
        qMeta.createTime = [[tmpDict objectForKey:@"CreateTime"] integerValue];
    }

    if([tmpDict objectForKey:@"DelayMessages"])
    {
        qMeta.delayMessages = [[tmpDict objectForKey:@"DelayMessages"] integerValue];
    }

    if([tmpDict objectForKey:@"DelaySeconds"])
    {
        qMeta.delaySeconds = [[tmpDict objectForKey:@"DelaySeconds"] integerValue];
    }

    if([tmpDict objectForKey:@"InactiveMessages"])
    {
        qMeta.inactiveMessages = [[tmpDict objectForKey:@"InactiveMessages"] integerValue];
    }

    if([tmpDict objectForKey:@"LastModifyTime"])
    {
        qMeta.lastModifyTime = [[tmpDict objectForKey:@"LastModifyTime"] integerValue];
    }

    if([tmpDict objectForKey:@"LoggingEnabled"])
    {
        if([[tmpDict objectForKey:@"LogginEnabled"] isEqualToString:@"True"])
        {
            qMeta.loggingEnable = 1;
        }
        else
        {
            qMeta.loggingEnable = 0;
        }
    }

    if([tmpDict objectForKey:@"MaximumMessageSize"])
    {
        qMeta.maximumMessageSize = [[tmpDict objectForKey: @"MaximumMessageSize"] integerValue];
    }

    if([tmpDict objectForKey:@"MessageRetentionPeriod"])
    {
        qMeta.messageRetentionPeriod = [[tmpDict objectForKey:@"MessageRetentionPeriod"] integerValue];
    }

    if([tmpDict objectForKey:@"PollingWaitSeconds"])
    {
        qMeta.pollingWaitSeconds = [[tmpDict objectForKey:@"PollingWaitSeconds"] integerValue];
    }

    if([tmpDict objectForKey:@"VisibilityTimeout"])
    {
        qMeta.visibilityTimeout = [[tmpDict objectForKey:@"VisibilityTimeout"] integerValue];
    }
}


+ (void) parseMessageFromDict:(NSDictionary*) dict
            withOutputMessage:(AliMNSMessage*) msg
{
    if([dict objectForKey:@"MessageId"])
    {
        msg.messageId = [dict objectForKey:@"MessageId"];
    }

    if([dict objectForKey:@"MessageBodyMD5"])
    {
        msg.messageBodyMd5 = [dict objectForKey:@"MessageBodyMD5"];
    }

    if([dict objectForKey:@"ReceiptHandle"])
    {
        msg.receiptHandle = [dict objectForKey:@"ReceiptHandle"];
    }
    if([dict objectForKey:@"MessageBody"])
    {
        msg.messageBody = [dict objectForKey:@"MessageBody"];
    }
    if([dict objectForKey:@"EnqueueTime"])
    {
        msg.enqueueTime = [[dict objectForKey:@"EnqueueTime"] integerValue];
    }
    if([dict objectForKey:@"NextVisibleTime"])
    {
        msg.nextVisibleTime = [[dict objectForKey:@"NextVisibleTime"] integerValue];
    }
    if([dict objectForKey:@"FirstDequeueTime"])
    {
        msg.firstDequeueTime = [[dict objectForKey:@"FirstDequeueTime"] integerValue];
    }
    if([dict objectForKey:@"DequeueCount"])
    {
        msg.dequeueCount = [[dict objectForKey:@"DequeueCount"] integerValue];
    }
    if([dict objectForKey:@"Priority"])
    {
        msg.priority = [[dict objectForKey:@"Priority"] integerValue];
    }
}

+(void) parseTopicMetaFromDict:(NSDictionary*) dict
           withOutputTopicMeta:(AliMNSTopicMeta*) tMeta
{
    if([dict objectForKey:@"TopicName"])
    {
        tMeta.topicName = [dict objectForKey:@"TopicName"];
    }

    if([dict objectForKey:@"CreateTime"])
    {
        tMeta.createTime = [[dict objectForKey:@"CreateTime"] integerValue];
    }

    if([dict objectForKey:@"LastModifyTime"])
    {
        tMeta.lastModifyTime = [[dict objectForKey:@"LastModifyTime"] integerValue];
    }

    if([dict objectForKey:@"MaximumMessageSize"])
    {
        tMeta.maximumMessageSize = [[dict objectForKey:@"MaximumMessageSize"] integerValue];
    }

    if([dict objectForKey:@"MessageRetentionPeriod"])
    {
        tMeta.messageRetentionPeriod = [[dict objectForKey:@"MessageRetentionPeriod"] integerValue];
    }

    if([dict objectForKey:@"MessageCount"])
    {
        tMeta.messageCount = [[dict objectForKey:@"MessageCount"] integerValue];
    }

    if([dict objectForKey:@"LoggingEnabled"])
    {
        if([[dict objectForKey:@"LogginEnabled"] isEqualToString:@"True"])
        {
            tMeta.loggingEnable = 1;
        }
        else
        {
            tMeta.loggingEnable = 0;
        }
    }
}

+(void) parseSubscriptionMetaFromDict:(NSDictionary*) dict
           withOutputSubscriptionMeta:(AliMNSSubscription*) subMeta
{
    if([dict objectForKey:@"Subscriber"])
    {
        subMeta.subscriber = [dict objectForKey:@"Subscriber"];
    }

    if([dict objectForKey:@"TopicName"])
    {
        subMeta.topicName = [dict objectForKey:@"TopicName"];
    }

    if([dict objectForKey:@"TopicOwner"])
    {
        subMeta.topicOwner = [dict objectForKey:@"TopicOwner"];
    }

    if([dict objectForKey:@"SubscriptionName"])
    {
        subMeta.subscriptionName = [dict objectForKey:@"SubscriptionName"];
    }

    if([dict objectForKey:@"CreateTime"])
    {
        subMeta.createTime = [[dict objectForKey:@"CreateTime"] integerValue];
    }

    if([dict objectForKey:@"Endpoint"])
    {
        subMeta.endpoint = [dict objectForKey:@"Endpoint"];
    }

    if([dict objectForKey:@"FilterTag"])
    {
        subMeta.filterTag = [dict objectForKey:@"FilterTag"];
    }

    if([dict objectForKey:@"LastModifyTime"])
    {
        subMeta.lastModifyTime = [[dict objectForKey:@"LastModifyTime"] integerValue];
    }

    if([dict objectForKey:@"NotifyContentFormat"])
    {
        NSString* format = [dict objectForKey:@"NotifyContentFormat"];
        if ([format isEqual:@"XML"])
        {
            subMeta.notifyContentFormat = ALIMNS_TOPIC_XML;
        }else if ([format isEqual:@"JSON"])
        {
            subMeta.notifyContentFormat = ALIMNS_TOPIC_JSON;
        }else if ([format isEqual:@"SIMPLIFIED"])
        {
            subMeta.notifyContentFormat = ALIMNS_TOPIC_SIMPLIFIED;
        }
    }

    if([dict objectForKey:@"NotifyStrategy"])
    {
        NSString* strategy = [dict objectForKey:@"NotifyStrategy"];
        if ([strategy isEqual:@"BACKOFF_RETRY"])
        {
            subMeta.notifyStrategy = ALIMNS_TOPIC_BACKOFF_RETRY;
        }else if ([strategy isEqual:@"EXPONENTIAL_DECAY_RETRY"])
        {
            subMeta.notifyStrategy = ALIMNS_TOPIC_EXPONENTIAL_DECAY_RETRY;
        }
    }
}


+ (void) getQueueListFromXml:(NSData*) xmlData
                withOutputMarker:(NSString**)marker
         withOutputQueueMetaList:(NSMutableArray**) qMetaList
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    NSArray* qlist = [xmlDoc arrayValueForKeyPath:@"Queue"];
    *marker = [xmlDoc stringValueForKeyPath:@"NextMarker"];

    for (int i = 0; i < qlist.count; i++)
    {
        NSDictionary* tmpDict = qlist[i];
        AliMNSQueueMeta* qMeta = [AliMNSQueueMeta new];
        if([tmpDict objectForKey:@"QueueURL"])
        {
            qMeta.queueURL = [tmpDict objectForKey:@"QueueURL"];
        }

        [self parseQueueMetaFromDict:tmpDict withOutputQueueMeta:qMeta];

        [*qMetaList addObject:qMeta];
    }
}

+ (void) getQueueMetaFromXml:(NSData*) xmlData
         withOutputQueueMeta:(AliMNSQueueMeta*)qMeta
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    [self parseQueueMetaFromDict:xmlDoc withOutputQueueMeta:qMeta];
}

+ (void) getMessageFromXml:(NSData*)xmlData
         withOutputMessage:(AliMNSMessage*) msg
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    [self parseMessageFromDict:xmlDoc withOutputMessage:msg];
}

+ (void) getMessageListFromXml:(NSData*) xmlData
         withOutputMessageList:(NSMutableArray*) msgList
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    NSArray* mList = [xmlDoc arrayValueForKeyPath:@"Message"];
    for (int i = 0; i < mList.count; i++)
    {
        NSDictionary* msgDict = mList[i];
        AliMNSMessage* msg = [AliMNSMessage new];
        [self parseMessageFromDict:msgDict withOutputMessage:msg];
        [msgList addObject:msg];
    }
}

+ (void) getTopicMetaFromXml:(NSData*) xmlData
         withOutputTopicMeta:(AliMNSTopicMeta*)tMeta
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    [self parseTopicMetaFromDict:xmlDoc withOutputTopicMeta:tMeta];
}

+ (void) getTopicListFromXml:(NSData*) xmlData
            withOutputMarker:(NSString**)marker
     withOutputTopicList:(NSMutableArray**) topicList
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    NSArray* qlist = [xmlDoc arrayValueForKeyPath:@"Topic"];
    *marker = [xmlDoc stringValueForKeyPath:@"NextMarker"];

    for (int i = 0; i < qlist.count; i++)
    {
        NSDictionary* tmpDict = qlist[i];
        AliMNSTopicMeta* tMeta = [AliMNSTopicMeta new];
        if([tmpDict objectForKey:@"TopicURL"])
        {
            tMeta.topicUrl = [tmpDict objectForKey:@"TopicURL"];
        }

        [*topicList addObject:tMeta];
    }
}

+ (void) getSubscriptionFromXml:(NSData*) xmlData
         withOutputSubscriptionMeta:(AliMNSSubscription*)subMeta
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    [self parseSubscriptionMetaFromDict:xmlDoc withOutputSubscriptionMeta:subMeta];
}

+ (void) getSubscriptionListFromXml:(NSData*) xmlData
            withOutputMarker:(NSString**)marker
         withOutputSubscriptionList:(NSMutableArray**) subList
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    NSArray* slist = [xmlDoc arrayValueForKeyPath:@"Subscription"];
    *marker = [xmlDoc stringValueForKeyPath:@"NextMarker"];

    for (int i = 0; i < slist.count; i++)
    {
        NSDictionary* tmpDict = slist[i];
        NSString* subUrl = [tmpDict objectForKey:@"SubscriptionURL"];
        [*subList addObject:subUrl];
    }
}

+ (void) getNextVisibilityFromXml:(NSData*) xmlData
     withOutputNextVisibilityTime:(NSString**) nextTime
          withOutputReceiptHandle:(NSString**) rHandle
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    *nextTime = [xmlDoc stringValueForKeyPath:@"NextVisibleTime"];
    *rHandle = [xmlDoc stringValueForKeyPath:@"ReceiptHandle"];
}

+ (void) getErrorCodeFromXml:(NSData*) xmlData
              withOutputCode:(NSString**) code
           withOutputMessage:(NSString**) message
         withOutputRequestId:(NSString**) requestId
            withOutputHostId:(NSString**) hostId
{
    NSDictionary* xmlDoc = [NSDictionary dictionaryWithXMLData:xmlData];
    *code = [xmlDoc stringValueForKeyPath:@"Code"];
    *message = [xmlDoc stringValueForKeyPath:@"Message"];
    *requestId = [xmlDoc stringValueForKeyPath:@"RequestId"];
    *hostId = [xmlDoc stringValueForKeyPath:@"HostId"];
}
@end
